package com.atguigu.msmservice.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.atguigu.msmservice.service.MsmService;
import com.atguigu.msmservice.utils.HttpUtils;
import org.apache.http.HttpResponse;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class MsmServiceImpl implements MsmService {

    //发送短信的方法
    @Override
    public boolean send(Map<String, Object> param, String phone) {

/**
 * 购买的阿里云短信服务，免费10条
 */
            String host = "https://dfsns.market.alicloudapi.com";
            String path = "/data/send_sms";
            String method = "POST";
            String appcode = "3a2e3b735369439ba0739d7ae9f2611e";
            Map<String, String> headers = new HashMap<String, String>();
            //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
            headers.put("Authorization", "APPCODE " + appcode);
            //根据API的要求，定义相对应的Content-Type
            headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            Map<String, String> querys = new HashMap<String, String>();
            Map<String, String> bodys = new HashMap<String, String>();
            bodys.put("content", JSONObject.toJSONString(param));
            bodys.put("phone_number", phone);
            bodys.put("template_id", "TPL_0000");


            try {
                /**
                 * 重要提示如下:
                 * HttpUtils请从
                 * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
                 * 下载
                 *
                 * 相应的依赖请参照
                 * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
                 */
                HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
                return true;
                // System.out.println(response.toString());
                //获取response的body
                //System.out.println(EntityUtils.toString(response.getEntity()));
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }

/**
 * 需申请认证的短信服务
 */
    //     if(StringUtils.isEmpty(phone)) return false;
    //
    //     DefaultProfile profile =
    //             DefaultProfile.getProfile("default", "LTAI4FvvVEWiTJ3GNJJqJnk7", "9st82dv7EvFk9mTjYO1XXbM632fRbG");
    //     IAcsClient client = new DefaultAcsClient(profile);
    //
    //     //设置相关固定的参数
    //     CommonRequest request = new CommonRequest();
    //     //request.setProtocol(ProtocolType.HTTPS);
    //     request.setMethod(MethodType.POST);
    //     request.setDomain("dysmsapi.aliyuncs.com");
    //     request.setVersion("2017-05-25");
    //     request.setAction("SendSms");
    //
    //     //设置发送相关的参数
    //     request.putQueryParameter("PhoneNumbers",phone); //手机号
    //     request.putQueryParameter("SignName","我的谷粒在线教育网站"); //申请阿里云 签名名称
    //     request.putQueryParameter("TemplateCode","SMS_180051135"); //申请阿里云 模板code
    //     request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param)); //验证码数据，转换json数据传递
    //
    //     try {
    //         //最终发送
    //         CommonResponse response = client.getCommonResponse(request);
    //         boolean success = response.getHttpResponse().isSuccess();
    //         return success;
    //     }catch(Exception e) {
    //         e.printStackTrace();
    //         return false;
    //     }
    //
    }
}
